<?php
namespace app\base\model;
use app\base\CommonModel;
use app\base\model\{
           OrderGoods,
           Attribute,
          AttributeValues
};
use think\db\Query;

class Order extends CommonModel{
   public function __construct($data = [])
   {
       parent::__construct($data);
   }
   /**
    *订单列表
    */
   public function getOrderList($user_id,$status,$pageNo,$pageSize){
       $where['o.user_id']=$user_id;
       $where['o.is_delete']=0;
       if($status!=0){
           $where['o.order_status']=$status;
       }
       $list=$this->alias('o')
                  ->where($where)
                  ->order('o.order_id','desc')
                  ->page($pageNo,$pageSize)
                  ->select();
       $order_goods_model=new OrderGoods();
       $attr_model=new Attribute();
       $attr_values_model=new AttributeValues();
       foreach($list as $key=>$value){
           $order_goods = $order_goods_model->alias('og')
               ->join('yc_goods g','g.goods_id=og.goods_id','left')
               ->join('yc_goods_attr_combination ga','ga.com_id=og.goods_attr_com_id','left')
               ->where(['og.user_id'=>$user_id,'og.order_id'=>$value['order_id']])
               ->field('og.id as og_id,og.goods_id,og.goods_num,og.goods_attr_com_id,og.goods_amount,og.goods_attr_com_id,g.goods_name,g.goods_thumb,ga.values_id')
               ->select();

           foreach($order_goods as $k=>$v){
               $goods_attr_arr=[];
               if(!empty($v['values_id'])){
                   $values_id_arr=explode(',',$v['values_id']);
                   for($i=0;$i<count($values_id_arr);$i++){
                       $value_info=$attr_values_model->where(['id'=>$values_id_arr[$i]])->find();
                       array_push($goods_attr_arr,$value_info['value']);
                   }
               }

               $order_goods[$k]['goods_attr']=$goods_attr_arr;
           }
           $list[$key]['goods_list']=$order_goods;
       }
       $total =$this->alias('o')
           ->where($where)
           ->order('o.order_id','desc')
           ->count();
       $result['list']=$list;
       $result['total']=$total;
       return $result;
   }
   /**
    *获取订单详情
    */
   public function getOrderInfo($user_id,$order_id){
       $info=$this->where(['user_id'=>$user_id,'order_id'=>$order_id])->find();
       $order_goods_model=new OrderGoods();
       $order_goods_list=$order_goods_model->alias('og')
                                           ->join('yc_goods g','g.goods_id=og.goods_id','left')
                                           ->join('yc_goods_attr_combination ga','ga.com_id=og.goods_attr_com_id','left')
                                           ->where(['og.user_id'=>$user_id,'og.order_id'=>$info['order_id']])
                                           ->field('og.id as og_id,og.goods_id,og.goods_num,og.goods_amount,og.goods_attr_com_id,g.goods_name,g.goods_thumb,ga.values_id')
                                           ->select();
       $attr_model=new Attribute();
       $attr_values_model=new AttributeValues();
       foreach($order_goods_list as $key=>$value){
             $goods_attr_arr=[];
              if(!empty($value['values_id'])){
                   $values_id_arr=explode(',',$value['values_id']);
                   for($i=0;$i<count($values_id_arr);$i++){
                       $value_info=$attr_values_model->where(['id'=>$values_id_arr[$i]])->find();
                       $attr_info =$attr_model ->where(['attr_id'=>$value_info['attr_id']])->find();
                       array_push($goods_attr_arr,$attr_info['attr_name'].':'.$value_info['value']);
                   }
              }
              $order_goods_list[$key]['goods_attr']=$goods_attr_arr;
       }
       $info['goods_list']=$order_goods_list;
       return $info;
   }

   /**
    *搜索订单
    */
   public function searchOrders($user_id,$keywords,$pageNo,$pageSize){
       $where['o.user_id']=$user_id;
       $where['o.is_delete']=0;
       $where['o.order_sn']=['like',"%$keywords%"];
       $whereOr['o.user_id']=$user_id;
       $whereOr['o.is_delete']=0;
       $whereOr['g.goods_name']=['like',"%$keywords%"];
       $list=$this->alias('o')
                        ->join('yc_order_goods og','og.order_id=o.order_id','left')
                        ->join('yc_goods g','og.goods_id=g.goods_id','left')
                       ->field('o.*,og.id as og_id,og.goods_id,og.goods_num,og.goods_attr_com_id,og.goods_amount,og.goods_attr_com_id,g.goods_name,g.goods_thumb')
                        ->where(function ($query)use($where){
                            $query->where($where);
                        })
                        ->whereOr(function($query)use($whereOr){
                            $query->where($whereOr);
                        })
                        ->group('o.order_id')
                        ->order('order_id','desc')
                        ->page($pageNo,$pageSize)
                        ->select();
       $order_goods_model=new OrderGoods();
       $attr_model=new Attribute();
       $attr_values_model=new AttributeValues();
       foreach($list as $key=>$value){
           $order_goods = $order_goods_model->alias('og')
               ->join('yc_goods g','g.goods_id=og.goods_id','left')
               ->join('yc_goods_attr_combination ga','ga.com_id=og.goods_attr_com_id','left')
               ->where(['og.user_id'=>$user_id,'og.order_id'=>$value['order_id']])
               ->field('og.id as og_id,og.goods_id,og.goods_num,og.goods_attr_com_id,og.goods_amount,og.goods_attr_com_id,g.goods_name,g.goods_thumb,ga.values_id')
               ->select();
           foreach($order_goods as $k=>$v){
               $goods_attr_arr=[];
               if(!empty($v['values_id'])){
                   $values_id_arr=explode(',',$v['values_id']);
                   for($i=0;$i<count($values_id_arr);$i++){
                       $value_info=$attr_values_model->where(['id'=>$values_id_arr[$i]])->find();
                       array_push($goods_attr_arr,$value_info['value']);
                   }
               }

               $order_goods[$k]['goods_attr']=$goods_attr_arr;
           }
           $list[$key]['goods_list']=$order_goods;
       }
       $list_total=$this->alias('o')
           ->join('yc_order_goods og','og.order_id=o.order_id','left')
           ->join('yc_goods g','og.goods_id=g.goods_id','left')
           ->field('o.*,og.id as og_id,og.goods_id,og.goods_num,og.goods_attr_com_id,og.goods_amount,og.goods_attr_com_id,g.goods_name,g.goods_thumb')
           ->where(function ($query)use($where){
               $query->where($where);
           })
           ->whereOr(function($query)use($whereOr){
               $query->where($whereOr);
           })
           ->group('o.order_id')
           ->order('order_id','desc')
           ->select();
       $total=count($list_total);
       $result['list']=$list;
       $result['total']=$total;
       return $result;
   }
  /**
   *支付宝的配置
   */
  public function alipay_config(){
      $config = array (
          //应用ID,您的APPID。
          'app_id' => "2019061465571163",

          //商户私钥
          'merchant_private_key' => "MIIEogIBAAKCAQEAqMiDgJ1y8sc+ytdiCR35AmMeN3F6fCfZmAHsGv43Zh5VXA7/GxFlot5q6MXorOp9wf8MRkEhPMZ86Hug3oXiuBflfRC3JJeAMCas5fZPKfx1Z4+lJwQkFg5270oHPKRMe5GfKDJ4604F1IbFdiRGCAPghStWHS5tC3x2wxtdCQmJWpVEsTzPVLPvj2sYuHs4p7C71GgUE3pg1HEWoqXEIeI3SH5rS2HtQe7idep4pgv9UlNqadt3FixyIxgG/nGeDCS2hnynjxjbh8/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fFnQqt0mj05zASQ070Qv8wf2wFaO6Lak6cXjIg5VV5BIzhi+kAMvA93J3gl8W0th+ONmZAoGAG2uQzFfxx5u+R2c7O+V4PnUlRH4x4flg5bGYQQZ3TRV2k5s+s627AWx+eOLN49B1B+k/Yj7yBFibqOTbmH1jqIvV3ClSdQ8YRx4PwtxmkoN8esynUONKpcRqgAl2UETLcWrkkMsfIx8rp2XkYjajTJzgxMbMtr4LsxenG1/K1Fw=",

          //异步通知地址
          'notify_url' => "http://www.sun9.top/api/pay/notify_url",

          //同步跳转
          'return_url' => "http://www.sun9.top/api/pay/return_url",

          //编码格式
          'charset' => "UTF-8",

          //签名方式
          'sign_type'=>"RSA2",

          //支付宝网关
          'gatewayUrl' => "https://openapi.alipay.com/gateway.do",

          //支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
          'alipay_public_key' => "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAgBoQERKVj5gBwths77HDl37l+rwAKqnCdYR1R+AoQELkIr1unI/v2xphdFJ2B2Z+tAlS2Yn0lVATqrxpsc2Vr4Nob8rUlz18qKtWkwf5r8v9RknjOqntWubemSDcIwej5F+c/TO4xsysDqp+zeFAr5xpRbu12eh1eBBmNPYGXx+VxX3WDJT3/2UcTRkWx8AedLL+b8w37X5pWfLlR8UrVfEjFVtpRdoEjBHysXHI88jCvVlJHsLEF3LPTx35XVLSGRKLRKCATh87zkulT/P4hVwJ7fcu3wN7j1zPKIV+g9Zrws/FvhYHMe73UVr5mvfNNrO/mtOOds0g/q26SAcrrwIDAQAB",
      );
      return $config;
  }

}